package com.kobako.controller;

import com.alibaba.fastjson.JSON;
import com.kobako.entity.BbsEntity;
import com.kobako.model.BBSDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by kobako on 2017/4/5.
 * Just a game
 */
@Controller
public class BbsController {
    @Autowired
    BBSDao bbsDao;

    @RequestMapping(value = "/bbsSubmit", method = RequestMethod.POST)
    public String submitBbs(@RequestParam(name = "name") String name,
                          @RequestParam(name = "comment") String comment,
                          @RequestParam(name = "emoji")String emoji) {
        BbsEntity bbs = new BbsEntity();
        bbs.setName(name);
        bbs.setComment(comment);
        bbs.setEmoji(emoji);
        bbsDao.save(bbs);
        return "index";
    }

    @ResponseBody
    @RequestMapping(value = "/getComments")
    public String getCommentByPage(@RequestParam(name = "page") int page) {
        System.out.println("Get comment by page : "+page);
        List<BbsEntity> comments = bbsDao.getCommentsByPage(page);
        return JSON.toJSONString(comments);
    }

    @ResponseBody
    @RequestMapping(value = "/getPageCount")
    public String getCommetsPageCount() {
        int pageCount = bbsDao.getPageCount();
        return String .valueOf(pageCount);
    }
}
